package com.z.xhelper.core.listener;

import static com.z.xhelper.core.utils.HelperUtil.hookSendTextMsg;

import com.z.xhelper.core.config.BeanContainer;
import com.z.xhelper.core.config.Consts;
import com.z.xhelper.core.dao.ServiceSocketMsg;
import com.z.xhelper.core.dao.WeChatMsg;
import com.z.xhelper.core.eventbus.IListener;
import com.z.xhelper.core.events.MsgSendEvent;
import com.z.xhelper.core.listener.sends.BindUidAction;
import com.z.xhelper.core.utils.HttpsUtil;
import com.z.xhelper.core.utils.LogUtil;

import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedHelpers;

public class SocketSendTextMsgListener implements IListener<MsgSendEvent> {

    @Override
    public Class<?> getEventName() {
        return MsgSendEvent.class;
    }

    @Override
    public void run(MsgSendEvent event) {
        String context = event.getContext();
        if (context == null){
            return;
        }
        // todo 用责任链模式重构
        if (BindUidAction.doAction(context)){
            return;
        }

        WeChatMsg weChatMsg = new WeChatMsg();
        try {
            JSONObject jsonObject = new JSONObject(context);
            weChatMsg.setTalker(jsonObject.getString("wxid"));
            weChatMsg.setContent(jsonObject.getString("msg"));
            weChatMsg.setType(jsonObject.getInt("type"));
        }catch (Exception e){
            LogUtil.log("解析socket发送的消息失败" + e.getMessage());
            return;
        }

        if(weChatMsg.getType() != 1){
            return;
        }

        // 发送文本消息
        hookSendTextMsg(weChatMsg);
    }



}
